package com.czxy.demo05;

import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Properties;

/**
 * Created by hp on 2020/2/2.
 */
public class Test27 {
    public static void main(String[] args) {

        jsBeabXML();
    }
    public static void jsBeabXML(){
        SAXReader sr = new SAXReader();
        try{
            Document doc = sr.read(new File("a.xml"));
            Element rootElement = doc.getRootElement();
            List<Element> elements = rootElement.elements();
            Properties car = new Properties();
            Properties book= new Properties();
            for (Element bean : elements) {
                String className = bean.attributeValue("className");
                if (className.equals("com.czxy.demo05.Car")){
                    List<Element> pro = bean.elements();
                    for (Element p : pro) {
                        String name = p.attributeValue("name");
                        String value = p.attributeValue("value");
                        BeanUtils.setProperty(car,name,value);
                    }
                }else{
                    List<Element> pro = bean.elements();
                    for (Element p : pro) {
                        String name = p.attributeValue("name");
                        String value = p.attributeValue("value");
                        BeanUtils.setProperty(book,name,value);
                    }
                }
            }
            System.out.println(car);
            System.out.println(book);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
